﻿package com.bj58.chr.sensitiveword.core;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * KeyWordFilter.java
 * 
 * @auther Boen
 * @date 2015年1月15日
 * @update 下午8:03:45
 */
public class SensitiveWordChecker {

    private static final Logger logger = LoggerFactory.getLogger(SensitiveWordChecker.class);

    private DoubleArrayTrie trie;

    private static volatile SensitiveWordChecker instance;

    public static SensitiveWordChecker getInstance() {
        if (instance == null) {
            synchronized (SensitiveWordChecker.class) {
                if (instance == null) {
                    instance = new SensitiveWordChecker();
                }
            }
        }
        return instance;
    }

    private SensitiveWordChecker() {
        try {
            trie = new DoubleArrayTrie();
            trie.loadKeyWord();
            String path = System.getProperty("user.dir") + "/conf/log4j.properties";
            PropertyConfigurator.configure(path);
            logger.info("init success");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public SensitiveResult matchKeyWord(String content) {
        List<String> keywords = SensitiveWordChecker.getInstance().trie.getAllKeyWord(content);
        SensitiveResult result = new SensitiveResult();
        result.setContain(keywords.size() > 0 ? true : false);
        result.setKeywords(keywords);
        return result;
    }

    public boolean iskeyWord(String keyword) {
        return SensitiveWordChecker.getInstance().trie.isKeyWord(keyword);
    }

}
